package com.mindainfo.groundwave.web.device.web;

import com.mindainfo.groundwave.common.api.device.service.IRelayService;
import com.mindainfo.groundwave.common.api.device.vo.RelayVo;
import com.mindainfo.groundwave.common.bean.CommonResult;
import com.mindainfo.groundwave.common.bean.DataGrid;
import org.apache.logging.log4j.core.util.datetime.FastDateFormat;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

/**
 * Created by MD-01 on 2017/5/18.
 */
//@Controller
@RequestMapping("/sys/relay")
public class RelayController {

    @Resource
    private IRelayService relayService;

    @RequestMapping("/page")
    public String page(){
        return "/device/relay";
    }

    @RequestMapping("/add")
    @ResponseBody
    public CommonResult<Void> add(String deviceId, Integer state, String maker, String version, String productionDate){

        FastDateFormat format = FastDateFormat.getInstance("yyyy-MM-dd");
        Date production = null;
        try {
            production = format.parse(productionDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return relayService.add(deviceId,state,maker,version,production);
    }

    @RequestMapping("/queryList")
    @ResponseBody
    public DataGrid<RelayVo> queryList(Integer page,Integer rows,String key){

        if(page==null){
            page = 1;
        }
        if(rows==null){
            rows = 10;
        }
        int start = (page-1)*rows;
        CommonResult<DataGrid<RelayVo>> result = relayService.queryList(start, rows, key);
        if(result.getCode()==0){
            return result.getData();
        }else{
            return null;
        }
    }
}
